<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/8/2  13:47
// +----------------------------------------------------------------------
namespace Service;

class ServiceContainer {

    private static $di;      //服务容器
    private static $services; //服务列表
    private static $instances; //已实例的服务列表
    function __construct() {
        //服务配置,此处config必须有才可下一步实例化
        self::$services=[
            'QrcodeService'=>'\\Service\\QrcodeService',
            'UserService'=>'\\Service\\UserService',
            'WechatService'=>'\\Service\\WechatService',
            'PublicService'=>'\\Service\\PublicService',
            'ExcelService'=>'\\Service\\ExcelService',
            'GeneralService'=>'\\Service\\GeneralService'
        ];
        if(!isset(self::$di)){
            self::$di=$this;
        }
        return self::$di;
    }
    //获取服务类
    public function get($service){
        if(!isset(self::$services[$service])){
            return null;
        }
        if(!isset(self::$instances[$service])){
            self::$instances[$service]=new self::$services[$service](self::$di);
        }
        return self::$instances[$service];
    }
    //
}